package com.ilop.sthome.ui.activity.device;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.example.common.base.BaseBindActivity;
import com.example.common.utils.InputFilterUtil;
import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.common.command.SendDeviceCommand;
import com.ilop.sthome.data.enums.SmartDevice;
import com.ilop.sthome.data.event.EventGatewayInfo;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.data.greenDao.RoomBean;
import com.ilop.sthome.ui.ota.OTAConstants;
import com.ilop.sthome.ui.ota.bean.OTADeviceInfo;
import com.ilop.sthome.ui.ota.business.OTAActivityBusiness;
import com.ilop.sthome.utils.ConfigureUtil;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.ilop.sthome.utils.greenDao.utils.RoomDaoUtil;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivityDeviceDetailBinding;

/**
 * @author skygge
 * @date 2019/11/08.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：设备详情
 */

public class DeviceDetailActivity extends BaseBindActivity<ActivityDeviceDetailBinding> {

    private String mDeviceName;
    private int mDeviceId;
    private DeviceInfoBean mDeviceInfo;
    private DeviceInfoBean mDevice;
    private SendDeviceCommand mSend;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_device_detail;
    }

    @Override
    protected void initialize() {
        super.initialize();
        mDeviceName = getIntent().getStringExtra(CommonId.KEY_DEVICE_NAME);
        mDeviceId = getIntent().getIntExtra(CommonId.KEY_DEVICE_ID, 0);
        mDevice = DeviceDaoUtil.getInstance().findGatewayByDeviceName(mDeviceName);
        mSend = new SendDeviceCommand(mContext, mDevice);
    }

    @Override
    protected void initView() {
        super.initView();
        if (mDeviceId == 0){
            mDeviceInfo = DeviceDaoUtil.getInstance().findGatewayByDeviceName(mDeviceName);
            mDBind.tvDeviceTitle.setText(TextUtils.isEmpty(mDeviceInfo.getNickName()) ? getString(R.string.ali_gateway) : mDeviceInfo.getNickName());
            mDBind.siDeviceId.setVisibility(View.GONE);
            mDBind.siGateway.setVisibility(View.GONE);
            mDBind.siProductName.setVisibility(View.GONE);
            mDBind.siDeviceName.getCheckedView().setImageResource(R.drawable.icon_copy_sel);
        }else {
            mDBind.siVersion.setVisibility(View.GONE);
            mDBind.siIotId.setVisibility(View.GONE);
            mDBind.siDeviceName.setVisibility(View.GONE);
            mDBind.siWifi.setVisibility(View.GONE);
            mDeviceInfo = DeviceDaoUtil.getInstance().findByDeviceId(mDeviceName, mDeviceId);
            if (TextUtils.isEmpty(mDeviceInfo.getSubdeviceName())){
                String title = getString(SmartDevice.getType(mDeviceInfo.getDevice_type()).getTypeStrId())+mDeviceId;
                mDBind.tvDeviceTitle.setText(title);
            }else {
                mDBind.tvDeviceTitle.setText(mDeviceInfo.getSubdeviceName());
            }
        }
    }

    @Override
    protected void initData() {
        super.initData();
        if (mDeviceId == 0) {
            mSend.queryOrSetGatewayInfo("00","00");
            OTAActivityBusiness mBusiness = new OTAActivityBusiness(mHandler);
            mBusiness.requestDeviceDetailInfo(mDevice.getIotId());
            mDBind.siIotId.setDetailText(mDeviceInfo.getIotId());
            if (mDeviceInfo.getOwned() != 1){
                mDBind.siRoom.setDetailText(getString(R.string.ali_gateway_share));
            }
        }else {
            DeviceInfoBean gateway = DeviceDaoUtil.getInstance().findGatewayByDeviceName(mDeviceInfo.getDeviceName());
            mDBind.siProductName.setDetailText(getString(SmartDevice.getType(mDeviceInfo.getDevice_type()).getTypeStrId()));
            mDBind.siDeviceId.setDetailText(String.valueOf(mDeviceInfo.getDevice_ID()));
            mDBind.siGateway.setDetailText(TextUtils.isEmpty(gateway.getNickName())?getString(R.string.ali_gateway):gateway.getNickName());
            if (gateway.getOwned() != 1){
                mDBind.siRoom.setDetailText(getString(R.string.ali_gateway_share));
            }
        }
        if (mDeviceInfo.getRoom_id() != null){
            RoomBean room = RoomDaoUtil.getInstance().getRoomByRoomId(mDeviceInfo.getRoom_id());
            if (room !=null) {
                if (room.getNickname() != null) {
                    mDBind.siRoom.setDetailText(room.getNickname());
                } else {
                    mDBind.siRoom.setDetailText(ConfigureUtil.roomName(mContext, room.getRid()));
                }
            }
        }
        mDBind.siDeviceName.setDetailText(mDeviceInfo.getDeviceName());
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.ivStatusBack.setOnClickListener(view -> finish());
        mDBind.siDeviceName.getCheckedView().setOnClickListener(v -> {
            boolean success = InputFilterUtil.copy(mContext, mDBind.siDeviceName.getDetailText());
            if (success){
                showToast(getString(R.string.success_copy));
            }
        });
    }

    @Override
    protected void initDataBus() {
        super.initDataBus();
        LiveDataBus.get().with(CommonId.KEY_EVENT_GATEWAY_INFO, EventGatewayInfo.class).observe(this, event -> {
            if (event!=null){
                Log.i(TAG, "onEventMainThread: gateway " +  event.getData_str1());
                Message message = new Message();
                message.what = 0x11;
                message.obj = event.getData_str1();
                mHandler.sendMessage(message);
            }
        });
    }

    @SuppressLint("HandlerLeak")
    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.i(TAG, "handleMessage: " + msg.what);
            if (OTAConstants.MINE_MESSAGE_RESPONSE_OTA_DEVICE_INFO_SUCCESS == msg.what) {
                OTADeviceInfo info = (OTADeviceInfo) msg.obj;
                if (null != info.otaFirmwareDTO) {
                    String currentVersion = info.otaFirmwareDTO.currentVersion;
                    mDBind.siVersion.setDetailText(currentVersion);
                }
            }else if(OTAConstants.MINE_MESSAGE_RESPONSE_OTA_DEVICE_DETAIL_INFO_SUCCESS==msg.what){
                mDBind.siVersion.setDetailText((String) msg.obj);
            }else if (0x11 == msg.what){
                mDBind.siWifi.setDetailText((String) msg.obj);
            }
        }
    };
}
